package Objects;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import Invaders.Invader;

public class Power {
	
	private final int dano = 1;
	private final int raio = 600;
	private final int MAX_TEMPO_EXEC = 30;
	private ArrayList<Invader> listaInvader;
	private Shooter shooter;
	private Score score;
	private boolean estaAtiva;
	private int tempoExecutando;
	private double mana;
	private Image icon;
	private Image iconManaBar = null;
	private Image iconManaBar1;
	private Image iconManaBar2;
	private Image iconManaBar3;
	private Image iconManaBar4;
	
	public Power(ArrayList<Invader> listaInavder, Shooter shooter, Score score) {
		this.listaInvader = listaInavder;
		this.shooter = shooter;
		this.score = score;
		estaAtiva = false;
		icon = new ImageIcon(getClass().getResource("/Sprites/powerloop.gif")).getImage();
		iconManaBar = new ImageIcon(getClass().getResource("/Sprites/manabar1.png")).getImage();
		iconManaBar1 = new ImageIcon(getClass().getResource("/Sprites/manabar1.png")).getImage();
		iconManaBar2 = new ImageIcon(getClass().getResource("/Sprites/manabar2.png")).getImage();
		iconManaBar3 = new ImageIcon(getClass().getResource("/Sprites/manabar3.png")).getImage();
		iconManaBar4 = new ImageIcon(getClass().getResource("/Sprites/manabar4.png")).getImage();
		mana = 100;
	}
	
	public void exucutar() {
		if(tempoExecutando <= MAX_TEMPO_EXEC && estaAtiva == true) {
			shooter.zeraContPosShoot();
			int shooterXm = shooter.getXm();
			int shooterYm = shooter.getYm();
			for(Invader i: listaInvader) {
				int invaderXm = i.getXm();
				int invaderYm = i.getYm();
				double distancia = Math.sqrt( Math.pow( shooterXm - invaderXm  , 2) + Math.pow( shooterYm - invaderYm  , 2));
				if(distancia <= raio ) {
					i.perdeLife(dano);
					score.addScore(i.getScore());
				}
			}
			mana=0;
			tempoExecutando++;
		}
		else {
			tempoExecutando = 0;
			estaAtiva = false;
		}
		
		if(mana >= 100) {
			iconManaBar = iconManaBar1;
		}
		else {
			if(mana > 70) {
				iconManaBar = iconManaBar2;
			}
			else {
				if(mana > 50) {
					iconManaBar = iconManaBar3;
				}
				else {
					if(mana > 0) {
						iconManaBar = iconManaBar4;
					}
				}
			}
		}
	}
	
	public void ativar() {
		if(mana >= 100) {
			estaAtiva = true;
		}
	}
	
	public void draw(Graphics g) {
		if(tempoExecutando <= MAX_TEMPO_EXEC && estaAtiva == true) {
			g.drawImage(icon,shooter.getX()-40,shooter.getY()-40,null);
		}
		g.drawImage(iconManaBar,270,605,null );
		
	}
	
	public void setShooter(Shooter shooter) {
		this.shooter = shooter;
	}
	
	public void encherMana() {
		mana+=10;
	}
}
